<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Auth extends Mycontroller {

    public $template = "basic";
    
    /**
    * Индексный экшен
    * 
    */
    public function action_index() {
        
        $auth = Auth::instance();
        
        if($auth -> logged_in()) {
            $data['logged_in'] = true; 
            Controller::redirect('main');   
        }
        else {
            $data['logged_in'] = false;   
            $data['login']     = false;
            $data['name']      = '';
            $data['pass']      = '';
            
            
            if(isset($_POST['submit_login'])) {
                $login    = Arr::get($_POST, 'login', '');
                $password = Arr::get($_POST, 'password', '');

                $data['login'] = false;    
                $data['name']  = $login;
                $data['pass']  = $password;
                
                if($auth -> login($login, $password)) {
                    $data['login'] = true;    
                    Controller::redirect('main'); 
                }
                else {
                    $data['login'] = false;     
                }
            } 
        }
        
        $this -> template -> content = View::factory('auth', $data);
    }
    
    public function action_hpass() {
        $auth = Auth::instance();
        
        $this -> template -> content = $auth-> hash_password('pavelvesta1');
    }
    
    public function action_logout() {
        $auth = Auth::instance();
        $auth -> logout();
        
        Controller::redirect('main'); 
    }

} // End Welcome
